<template>
  <div class="table-page">
    <GiTable
      ref="tableRef"
      row-key="id"
      title="测试报告管理"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
        <a-input v-model="queryForm.version" placeholder="请输入乐观锁" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-input v-model="queryForm.createdBy" placeholder="请输入创建者" allow-clear @change="search">
          <template #prefix><icon-search /></template>
        </a-input>
        <a-button @click="reset">重置</a-button>
      </template>
      <template #custom-right>
        <a-button v-permission="['testing:report:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <span>新增</span>
        </a-button>
        <a-tooltip content="导出">
          <a-button v-permission="['testing:report:export']" class="gi_hover_btn-border" @click="onExport">
            <template #icon>
              <icon-download />
            </template>
          </a-button>
        </a-tooltip>
      </template>
      <template #name="{ record }">
        <a-link @click="onDetail(record)">{{ record.name }}</a-link>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['testing:report:update']" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['testing:report:delete']"
            status="danger"
            :disabled="record.disabled"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <ReportAddModal ref="ReportAddModalRef" @save-success="search" />
    <ReportDetailDrawer ref="ReportDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import ReportAddModal from './ReportAddModal.vue'
import ReportDetailDrawer from './ReportDetailDrawer.vue'
import { type ReportResp, type ReportQuery, deleteReport, exportReport, listReport } from '@/apis'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'Report' })

const queryForm = reactive<ReportQuery>({
  version: '',
  createdBy: '',
  sort: ['createdAt,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listReport({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: '', dataIndex: 'id', slotName: id },
  { title: '所属项目ID', dataIndex: 'projectId', slotName: projectId },
  { title: '', dataIndex: 'caseId', slotName: caseId },
  { title: '报告类型', dataIndex: 'type', slotName: type },
  { title: '报告名称', dataIndex: 'name', slotName: name },
  { title: '执行状态', dataIndex: 'executeState', slotName: executeState },
  { title: '', dataIndex: 'summary', slotName: summary },
  { title: '开始时间', dataIndex: 'startTime', slotName: startTime },
  { title: '结束时间', dataIndex: 'endTime', slotName: endTime },
  { title: '消耗时间', dataIndex: 'expandTime', slotName: expandTime },
  { title: '步骤数量', dataIndex: 'quantity', slotName: quantity },
  { title: '通过数量', dataIndex: 'passQuantity', slotName: passQuantity },
  { title: '失败数量', dataIndex: 'failQuantity', slotName: failQuantity },
  { title: '乐观锁', dataIndex: 'version', slotName: version },
  { title: '业务状态', dataIndex: 'status', slotName: status },
  { title: '逻辑删除 1: 已删除， 0: 未删除', dataIndex: 'isDeleted', slotName: isDeleted },
  { title: '创建者', dataIndex: 'createdBy', slotName: createdBy },
  { title: '更新者', dataIndex: 'updatedBy', slotName: updatedBy },
  { title: '创建时间', dataIndex: 'createdAt', slotName: createdAt },
  { title: '更新时间', dataIndex: 'updatedAt', slotName: updatedAt },
  {
    title: '操作',
    slotName: 'action',
    width: 130,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['testing:report:update', 'testing:report:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.version = ''
  queryForm.createdBy = ''
  search()
}

// 删除
const onDelete = (item: ReportResp) => {
  return handleDelete(() => deleteReport(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportReport(queryForm))
}

const ReportAddModalRef = ref<InstanceType<typeof ReportAddModal>>()
// 新增
const onAdd = () => {
  ReportAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (item: ReportResp) => {
  ReportAddModalRef.value?.onUpdate(item.id)
}

const ReportDetailDrawerRef = ref<InstanceType<typeof ReportDetailDrawer>>()
// 详情
const onDetail = (item: ReportResp) => {
  ReportDetailDrawerRef.value?.onDetail(item.id)
}
</script>

<style lang="scss" scoped></style>
